package controller

import (
	"errors"
	"fmt"
	"ordersystem/config"
	"ordersystem/model"
	"ordersystem/util"

	"github.com/gofiber/fiber/v2"
)

func OrderCreateController(ctx *fiber.Ctx) error {

	var order model.Order
	ctx.BodyParser(&order)

	fmt.Println("4", order)

	cookie := util.GetCookieForMap(ctx)

	order.UserId = cookie["id"].(int32)

	if err := order.CreateOrder(); err != nil {
		return err
	}

	util.SendResponse(ctx, nil)
	return nil
}

func OrderQueryOrderController(ctx *fiber.Ctx) error {
	var order model.Order
	ctx.BodyParser(&order)
	data, er := order.QueryOrder()
	if er != nil {
		return er
	}

	util.SendResponse(ctx, data)

	return nil
}

func OrderQueryOrdersController(ctx *fiber.Ctx) error {
	var page PageParam
	ctx.BodyParser(&page)

	cookie := util.GetCookieForMap(ctx)

	var isAdmin bool
	if cookie["identify"] == int32(2) {
		isAdmin = false
	} else {
		isAdmin = true
	}

	var userId int32 = 0
	if cookie["identify"] != int32(0) {
		userId = cookie["id"].(int32)
	}

	results, err := model.QueryOrders(userId, isAdmin, page.Page-1)

	if err != nil {
		return err
	}
	util.SendResponse(ctx, results)
	return nil
}

func OrderUpdateOrderSateController(ctx *fiber.Ctx) error {
	var order model.Order
	ctx.BodyParser(&order)
	cookie := util.GetCookieForMap(ctx)

	if cookie["identify"] == int32(2) {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	if order.State == 1 {
		if cookie["identify"] == int32(0) {
			if err := order.UpdateOrder(); err != nil {
				fmt.Println(err)
				return err
			}
			util.SendResponse(ctx, nil)
			return nil
		}

		if cookie["identify"] == int32(1) {
			orderData, _ := order.QueryOrder()
			if orderData["creator"] != int64(cookie["id"].(int32)) {
				return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
			} else {
				order.UpdateOrder()
				util.SendResponse(ctx, nil)
				return nil
			}
		}

	} else {
		if cookie["identify"] == int32(0) {
			if err := order.UpdateOrderToActive(); err != nil {
				return err
			} else {
				util.SendResponse(ctx, nil)
				return nil
			}
		} else {
			result, _ := order.QueryOrder()
			if result["creator"] != int64(cookie["id"].(int32)) {
				return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
			} else {
				if err := order.UpdateOrderToActive(); err != nil {
					return err
				} else {
					util.SendResponse(ctx, nil)
					return nil
				}
			}
		}

	}

	util.SendResponse(ctx, nil)

	return nil
}

type RequestPackageAboutYear struct {
	Year int32 `json:"year"`
}

// 只有超级管理员才能按年份删除预约信息
func OrderDeleteByYearController(ctx *fiber.Ctx) error {
	var year RequestPackageAboutYear
	ctx.BodyParser(&year)

	cookie := util.GetCookieForMap(ctx)

	if cookie["identify"] != int32(0) {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	model.DeleteOrderByYear(year.Year)

	util.SendResponse(ctx, nil)

	return nil
}

// 只有申请者才能删除预约信息
func OrderDeleteOrderController(ctx *fiber.Ctx) error {
	var order model.Order
	ctx.BodyParser(&order)

	data, err := order.QueryOrder()
	if err != nil {
		return err
	}

	cookie := util.GetCookieForMap(ctx)

	if cookie["id"] != data["user_id"] {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	if err := order.DeleteOrder(); err != nil {
		return err
	}
	util.SendResponse(ctx, nil)

	return nil
}

func OrderQueryNumberController(ctx *fiber.Ctx) error {

	cookie := util.GetCookieForMap(ctx)

	var isAdmin bool

	if cookie["identify"] == int32(2) {
		isAdmin = false
	} else {
		isAdmin = true
	}

	var userId int32 = 0
	if cookie["identify"] != int32(0) {
		userId = cookie["id"].(int32)
	}

	number := model.QueryOrderNumber(userId, isAdmin)

	util.SendResponse(ctx, number)
	return nil
}
